

#include "page_manager.h"
#include <stdio.h>
#include "config.h"
#include "disp_manager.h"

#include <string.h>
#include <math.h>
#include "ui.h"

static tButton g_Buttons[ItemConfig_number];
static int g_tButtonCount;
#include <unistd.h>


#define X_GAP 5
#define Y_GAP 5



 int MainChecked(struct Button * pbutton,DispBuff_p pDisbuff, inputEvent_p inputEventp)
{

	static unsigned int color = BUTTON_DEFAULT_COLOR;
	char  name[100];
	char  status[100];

	char *strbutton = NULL;
	strbutton = pbutton->name;

	//触摸屏事件，
	if(inputEventp->type == type_touch_event)
	{

		if(GetItemConfigByName(pbutton->name)->bcanbetouched == 0)
		{
				return -1;

		}

		//pbutton->status = !pbutton->status;
		printf("ASDWDWEWE------------------------------%d", inputEventp->pressure);
		if(inputEventp->pressure == 0)
		{

				if(color == BUTTON_PRESSED_COLOR)
				{
					color = BUTTON_DEFAULT_COLOR;
				}

				else 
				{
				
					color =BUTTON_PRESSED_COLOR ;
				}

		}


	}

	
	//对于网络事件
	else if(inputEventp->type == type_net_event)
	{
		sscanf(inputEventp->net_str, "%s %s", name, status);
		if(strcmp(status, "OK")==0)
		{

			color = BUTTON_PRESSED_COLOR;
		
		}
		else if(status[0] >='0' && status[0]<='9')
		{

			color = BUTTON_ADVANCE_COLOR;

			strbutton = status;
		}
		else if(strcmp(status, "ERR")==0)
		{
			color = BUTTON_DEFAULT_COLOR;

		}
		else
		{
			return -1;

		}

	}
	else
	{
		return -1;
	}



	



	DrawRegion(&pbutton->tRegion, color);
	
	DrawTextInRegionCentral(strbutton, &pbutton->tRegion, BUTTON_TEXT_COLOR);
	
	FlushDisplayRegion(&pbutton->tRegion, *pDisbuff);



	
	return 0;

}


 

static void CreateButtonGui(void)
{
	DispBuff_p Dispbuffp;

	int width,heigh;
	int n_per_line;
	int all_number;

	int xres,yres;

	int start_x ,start_y;
	int row=0,rows=0,cols=0;
	pButton pbutton;

	int prestart_x,prestart_y;
	int i=0;
	
	all_number = GetItemConfigCount();
	g_tButtonCount = all_number;
	
	Dispbuffp = get_initdisbuffer();

	
	xres = Dispbuffp->xres;
	yres = Dispbuffp->yres;

	//每个按钮的宽和高成黄金比例
//算出单个按钮的宽/高	
	width = sqrt(1.0/ 0.618*xres * yres  / all_number);
	n_per_line = xres / width + 1;

	width = xres / n_per_line;
	heigh = 0.618 * width;
	



//第一个按钮的起始坐标
	start_x = (xres-width*n_per_line)/2;
	rows =  all_number / n_per_line;
	if(rows * n_per_line < all_number)
		rows++;

	start_y = (yres-heigh*rows)/2;

	


//计算每个按钮的region		

	for(row = 0 ;(row < rows) && (i < all_number); row++)
	{

		prestart_y = start_y +row*heigh;
		prestart_x = start_x - width;

		
		for(cols = 0 ;(cols<n_per_line) && (i<all_number); cols++)
		{

			pbutton = &g_Buttons[i];
			pbutton->tRegion.leftup_x = prestart_x +width;
			pbutton->tRegion.leftup_y = prestart_y;
			pbutton->tRegion.width = width -X_GAP;
			pbutton->tRegion.heigh = heigh -Y_GAP;

			prestart_x = pbutton->tRegion.leftup_x;
			InitButton(GetItemConfigByIndex(i)->name, pbutton, NULL, NULL, MainChecked);
			i++;

		}
	}


	

	for(i = 0 ;i< all_number; i++)
	{

		
		g_Buttons[i].DrawButton(&g_Buttons[i], Dispbuffp);



	}
//居中显示







}

static int TouchPointInRegion(int x, int y, Region_p Regionp)
{
	if(x>Regionp->leftup_x && x<(Regionp->leftup_x + Regionp->width) )
	{
		if(y>Regionp->leftup_y && y<(Regionp->leftup_y + Regionp->heigh) )
		{
			return 1;
		}
	}
	else
	{
		return 0;
	}

	return 0;
}

static pButton NetNameInRegion(char *name)
{
	int i;

		
	for(i=0;i<g_tButtonCount;i++)
	{
		if(strcmp(name, g_Buttons[i].name)==0)
			return &g_Buttons[i];

		

	}


	return NULL;

}






pButton GetButtonByInputEvent(inputEvent_p inputEventp)
{

	int i;
	char name[100];
	if(inputEventp ->type == type_touch_event)
	{
		for(i=0;i<g_tButtonCount;i++)
		{
			if(TouchPointInRegion(inputEventp->x, inputEventp->y, &g_Buttons[i].tRegion))
				return &g_Buttons[i];

			

		}
		
		printf("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);


	}
	else if(inputEventp ->type == type_net_event)
	{
		sscanf(inputEventp->net_str, "%s", name);

		printf("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
		return NetNameInRegion(name);

		
		

	}

	return NULL;

}





void Main_run(void * pParam)
{

	pButton Pbutton =NULL;
	DispBuff_p Dispbuffp;
	int err;



	printf("MAIN Page\n");

	printf("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	inputEvent getinput;

	int error;

	Dispbuffp = get_initdisbuffer();
	error  = ParseConfig();
	if(error)
	{
		printf("ParseConfig error \n");
		return;

	}

	CreateButtonGui();

	while(1)
	{

		
		err = GetALLInputEvent(&getinput);
		if(err)
		{
				continue;
		}

		Pbutton = GetButtonByInputEvent(&getinput);
		
		if(Pbutton == NULL)
		{
			continue;
			

		}
		else
		{

		
				Pbutton->Checked(Pbutton,Dispbuffp, &getinput);
		
		

			
		}
		
	}

}




 struct page_action MainPageAction = {

	.name="main",
	.run=Main_run,
};



void MainPageRegister(void)
{

	Regsiterpage(&MainPageAction);


}























